package com.czl.module_service.fragment.assetUse

import com.alibaba.android.arouter.facade.annotation.Route
import com.bigkoo.pickerview.view.TimePickerView
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.TimeUtils
import com.czl.base.base.BaseFragment
import com.czl.base.config.AppConstants
import com.czl.base.data.bean.tengyun.SortBean
import com.czl.base.event.LiveBusCenter
import com.czl.base.util.DialogHelper
import com.czl.module_service.BR
import com.czl.module_service.R
import com.czl.module_service.adapter.asset.BorrowSortChoosedAdapter
import com.czl.module_service.databinding.*
import com.czl.module_service.viewmodel.assetUse.BorrowApplyAddViewModel
import com.lxj.xpopup.util.KeyboardUtils
import java.util.*

@Route(path = AppConstants.Router.Service.F_SERVICE_BORROW_APPLY_ADD)
class BorrowApplyAddFragment : BaseFragment<FragmentBorrowApplyAddBinding, BorrowApplyAddViewModel>() {

    val list : MutableList<SortBean> = arrayListOf()
    private lateinit var pvTime: TimePickerView

    private val headerBinding: FragmentBorrowApplyAddHeaderBinding by lazy  {
        FragmentBorrowApplyAddHeaderBinding.inflate(layoutInflater).apply {

        }
    }

    private val footerBinding: FragmentBorrowApplyAddFooterBinding by lazy  {
        FragmentBorrowApplyAddFooterBinding.inflate(layoutInflater).apply {

        }
    }

    override fun initContentView(): Int {
        return R.layout.fragment_borrow_apply_add
    }

    override fun initVariableId(): Int {
        return BR.viewModel
    }

    override fun useBaseLayout(): Boolean {
        return false
    }

    private val mAdapter: BorrowSortChoosedAdapter by lazy {
        BorrowSortChoosedAdapter(this).apply {
            headerBinding.setVariable(initVariableId(), viewModel)
            addHeaderView(headerBinding.root)
            footerBinding.setVariable(initVariableId(), viewModel)
            addFooterView(footerBinding.root)
            pvTime = DialogHelper.showDatePickDialog(requireContext(), "日期", Calendar.getInstance(), Calendar.getInstance(), null) { date, _ ->
                headerBinding.tvTouchTime.text = TimeUtils.date2String(date, "yyyy-MM-dd")
            }
        }
    }

    override fun initData() {
        super.initData()
        initAdapter()
        viewModel.getSortList()
        viewModel.getRoleProcessList()
        viewModel.adapter = mAdapter
    }

    private fun initAdapter() {
        binding.recyclerView.run {
            adapter = mAdapter
        }
    }

    override fun initViewObservable() {
        LiveBusCenter.observeAddChoosedSortEvent(this, {
            LogUtils.e("----->" + it.sortBean)
            if (it.sortBean != null) {
                // 选中的物品添加
                val index = mAdapter.position - 1
                list[index] = it.sortBean!!
                viewModel.list = list
                mAdapter.setList(list)
            }
        })
        viewModel.uc.loadCompleteEvent.observe(this, { data ->
            list.add(data[0])
            viewModel.list = list
            LogUtils.e("----->" + list.size)
            if (list.size > 1) {
                mAdapter.isDel = true
            }
            mAdapter.setList(list)
        })
        viewModel.uc.roleCompleteEvent.observe(this, { data ->
            if (data.isNotEmpty()) {
                viewModel.businessType = 0
                footerBinding.btnCommit.text = "提交审批"
            } else {
                viewModel.businessType = 1
                footerBinding.btnCommit.text = "提交"
            }
        })
        // 接触日期选择
        viewModel.uc.onReturnTimeEvent.observe(this, {
            if (headerBinding.tvTouchTime.text.toString().matches(Regex("^\\d*-\\d*-\\d*\$"))){
                val date = headerBinding.tvTouchTime.text.toString().split("-")
                pvTime.setDate(Calendar.getInstance().apply { this[date[0].toInt(),(date[1].toInt()-1)]=date[2].toInt() })
            }
            KeyboardUtils.hideSoftInput(headerBinding.tvTouchTime)
            pvTime.show()
        })
        viewModel.uc.clearFacilityListEvent.observe(this, {
            viewModel.list.clear()
            viewModel.note.set("")
            viewModel.returnBackTime.set("")
            val list1 : MutableList<SortBean> = arrayListOf()
            list1.add(SortBean())
            mAdapter.setList(list1)
        })

    }

    fun deleteList(sortBean: SortBean) {
        list.removeAt(sortBean.position!! - 1)
        if (list.size == 1) {
            mAdapter.isDel = false
        }
        mAdapter.setList(list)
    }

}